package cn.changsheng.csfenjie.utils;

import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitRunnable;

import cn.changsheng.csfenjie.main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

/**
* @author hs
* @Description: {签名验证}
* @date 2020/7/17 15:03
*/
public class TheOriginalCheckUtil {
    private final static String HTTP_DOMAIN = "http://mcadmin.ljxmc.top";
    private final static String VERIFY_SIGN = "/api/public/verifySign";
    /**
     * 平台上注册的插件名称
     */
    private final static String PLUGIN_NAME = "DaKong";
    /**
     * 平台上获取的插件对应秘钥
     */
    private final static String SECRET_KEY = "1295422278472962048";

    /**
     * 正版签名验证
     */
    public static void theOriginalCheck() {
        String sign = "1295430094503940096";
                // 获取服务器端口
                int port = main.getInstance().getServer().getPort();
        // 进行校验
        new BukkitRunnable() {
            @Override
            public void run() {
                try {
                    String urlStr = HTTP_DOMAIN + VERIFY_SIGN +
                            "?sign=" + sign +
                                                        "&port=" + port +
                            "&pluginName=" + PLUGIN_NAME +
                            "&secretKey=" + SECRET_KEY;
                    URL url = new URL(urlStr);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
                    StringBuilder stringBuilder = new StringBuilder();
                    String current;
                    while ((current = in.readLine()) != null) {
                        stringBuilder.append(current);
                    }
                    String verifySign = stringBuilder.toString();
                    if ("true".equals(verifySign)) {
                        // TODO 验证成功进行操作
                    	System.out.println("成功");
                    	System.out.println("成功");
                    	System.out.println("成功");
                    	System.out.println("成功");
                    	System.out.println("成功");
                    	System.out.println("成功");
                    	System.out.println("成功");
                    	System.out.println("成功");
                    } else {
                        // TODO 验证失败进行操作
                    	System.out.println("验证失败");
                    	System.out.println("验证失败");
                    	System.out.println("验证失败");
                    	System.out.println("验证失败");
                    	System.out.println("验证失败");
                    	System.out.println("验证失败");
                    	System.out.println("验证失败");
                    }
                    this.cancel();
                } catch (IOException e) {
                    // TODO 网络或者其他问题进行打印消息提醒,并会自动一分钟后重试
                }
            }
        }.runTaskTimerAsynchronously(main.getInstance(), 0, 20 * 60);
    }

}